﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Threading;
using AMicroblogAPI;
using AMicroblogAPI.Common;
using AMicroblogAPI.DataContract;

namespace Comment
{
    public partial class Form1 : Form
    {
        private Decimal pbvalue;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            pbvalue = 0;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.DefaultExt = ".db";
            saveFile.ShowDialog();
            tbFileName.Text = saveFile.FileName;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbFileName.Text.Trim()))
            {
                MessageBox.Show("File name must be specified!");
                tbFileName.Focus();
                return;
            }
            SQLiteConnection.CreateFile(tbFileName.Text.Trim());
            Int64 start = Int64.Parse(numstart.Value.ToString());
            Int64 end = Int64.Parse(numend.Value.ToString());
            if (start > end)
            {
                Int64 temp = start;
                start = end;
                end = temp;
            }
            numstart.Enabled = false;
            numend.Enabled = false;
            btnOk.Enabled = false;
            btnCancel.Enabled = false;
            btnSelect.Enabled = false;
            tbFileName.Enabled = false;
            progressBar1.Value = 0;
            new Thread(() =>
            {
                Control.CheckForIllegalCrossThreadCalls = false;
                SaveTopics(tbFileName.Text, start, end);
                SaveComments(tbFileName.Text, start, end);
                progressBar1.Value = 100;
                MessageBox.Show("Finished!");
                btnOk.Enabled = true;
                btnCancel.Enabled = true;
                btnSelect.Enabled = true;
                tbFileName.Enabled = true;
                numstart.Enabled = true;
                numend.Enabled = true;
            }).Start();
            // this.Close();
        }

        private void SaveComments(string p, long start, long end)
        {
            try
            {
                AMicroblog.Login("chenmichael0@gmail.com", "alphad");
                SQLiteConnection connection = new SQLiteConnection("data source = " + p);
                connection.Open();
                SQLiteCommand command = connection.CreateCommand();
                command.CommandText = "create table comments (id long, comment nvarchar(1000))";
                command.ExecuteNonQuery();
                for (long index = start; index <= end; index++)
                {
                    pbvalue += Decimal.Parse("70") / Decimal.Parse((end - start).ToString());
                    if (progressBar1.Value + (int)pbvalue > 90)
                    {
                        progressBar1.Value = 90;
                    }
                    else
                        progressBar1.Value += (int)pbvalue;
                    try
                    {
                        Comments comment = AMicroblog.GetComments(index);
                        for (int i = 0; i < comment.Items.Count(); i++)
                        {
                            string text = comment.Items[i].Text;
                            command.CommandText = "insert into comments (id, comment) values (" + index.ToString() + ", '" + text + "')";
                            command.ExecuteNonQuery();
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                connection.Close();
                AMicroblog.Logout();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SaveTopics(string p, long start, long end)
        {
            try
            {
                AMicroblog.Login("chenmichael0@gmail.com", "alphad");
                SQLiteConnection connection = new SQLiteConnection("data source = " + p);
                connection.Open();
                SQLiteCommand command = connection.CreateCommand();
                command.CommandText = "create table topics (id long, topic nvarchar(1000))";
                command.ExecuteNonQuery();
                for (long index = start; index <= end; index++)
                {
                    pbvalue += Decimal.Parse("30") / Decimal.Parse((end - start).ToString());
                    if (progressBar1.Value + (int)pbvalue > 30)
                    {
                        progressBar1.Value = 30;
                    }
                    else
                        progressBar1.Value += (int)pbvalue;
                    try
                    {
                        StatusInfo status = AMicroblog.GetStatus(index);

                        if (status != null)
                        {
                            string text = (status.Text);
                            command.CommandText = "insert into topics (id, topic) values (" + index.ToString() + ", '" + text + "')";
                            command.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                     //   MessageBox.Show(ex.Message);
                        continue;
                    }
                }
                connection.Close();
                AMicroblog.Logout();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}